package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;


import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_CHECK;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_END_HOUR;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_END_MIN;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_START_HOUR;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_START_MIN;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SCREENBRIGHT_VALUE;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.NumberPicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.suke.widget.SwitchButton;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.Calendar;

public class SpecifiedProximityTracing extends DialogFragment implements View.OnClickListener {
    private Context mContext;
    private ConfigCallBack mConfigCallback;
    private int startYear = 0;
    private int startMon = 0;
    private int startDay = 0;
    private int startHour = 0;
    private int startMin = 0;
    private int startSec = 0;
    private int endYear = 0;
    private int endMon = 0;
    private int endDay = 0;
    private int endHOUR = 0;
    private int endMin = 0;
    private int endSec = 0;

    NumberPicker startYearPick;
    NumberPicker startMonPick;
    NumberPicker startDayPick;
    NumberPicker startHourPick;
    NumberPicker startMinPick;
    NumberPicker startSecPick;
    NumberPicker endYearPick;
    NumberPicker endMonPick;
    NumberPicker endDayPick;
    NumberPicker endHourPick;
    NumberPicker endMinPick;
    NumberPicker endSecPick;
    Calendar calendar = Calendar.getInstance();




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = inflater.getContext();
        View view = inflater.inflate(R.layout.config_proximitytracing, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        startYearPick = view.findViewById(R.id.s_year);
        startMonPick = view.findViewById(R.id.s_month);
        startDayPick = view.findViewById(R.id.s_day);
        startHourPick = view.findViewById(R.id.s_hour);
        startMinPick = view.findViewById(R.id.s_min);
        startSecPick = view.findViewById(R.id.s_sec);
        endYearPick = view.findViewById(R.id.e_year);
        endMonPick = view.findViewById(R.id.e_month);
        endDayPick = view.findViewById(R.id.e_day);
        endHourPick = view.findViewById(R.id.e_hour);
        endMinPick = view.findViewById(R.id.e_min);
        endSecPick = view.findViewById(R.id.e_sec);

        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        int curDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        startYearPick.setMinValue(curYear - 10);
        startYearPick.setMaxValue(curYear + 10);
        startYearPick.setValue(calendar.get(Calendar.YEAR));

        startMonPick.setMinValue(1);
        startMonPick.setMaxValue(12);
        startMonPick.setValue(calendar.get(Calendar.MONTH) + 1);

        startDayPick.setMinValue(1);
        startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
        startDayPick.setValue(curDay);

        startHourPick.setMinValue(0);
        startHourPick.setMaxValue(23);
        startHourPick.setValue(calendar.get(Calendar.HOUR_OF_DAY));

        startMinPick.setMinValue(0);
        startMinPick.setMaxValue(59);
        startMinPick.setValue(calendar.get(Calendar.MINUTE));

        startSecPick.setMinValue(0);
        startSecPick.setMaxValue(59);
        startSecPick.setValue(calendar.get(Calendar.SECOND));

        startYearPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDayPick.setMinValue(1);
                startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
                startDay = picker.getValue();
            }
        });
        startMonPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDayPick.setMinValue(1);
                startDayPick.setMaxValue(getDayTS(getYear(),getMonth()));
                startMon = picker.getValue();
            }
        });
        startDayPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startDay = picker.getValue();
                Log.i("TAG", "onValueChange: " + picker.getValue());
            }
        });
        startHourPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startHour = picker.getValue();
            }
        });
        startMinPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startMin = picker.getValue();
            }
        });
        startSecPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                startSec = picker.getValue();
            }
        });

        endYearPick.setMinValue(curYear - 10);
        endYearPick.setMaxValue(curYear + 10);
        endYearPick.setValue(calendar.get(Calendar.YEAR));

        endMonPick.setMinValue(1);
        endMonPick.setMaxValue(12);
        endMonPick.setValue(calendar.get(Calendar.MONTH) + 1);

        endDayPick.setMinValue(1);
        endDayPick.setMaxValue(getDayTS(getEndYear(),getEndMonth()));
        endDayPick.setValue(curDay);

        endHourPick.setMinValue(0);
        endHourPick.setMaxValue(23);
        endHourPick.setValue(calendar.get(Calendar.HOUR_OF_DAY));

        endMinPick.setMinValue(0);
        endMinPick.setMaxValue(59);
        endMinPick.setValue(calendar.get(Calendar.MINUTE));

        endSecPick.setMinValue(0);
        endSecPick.setMaxValue(59);
        endSecPick.setValue(calendar.get(Calendar.SECOND));

        endYearPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                endDayPick.setMinValue(1);
                endDayPick.setMaxValue(getDayTS(getEndYear(),getEndMonth()));
                endYear = picker.getValue();
            }
        });
        endMonPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                endDayPick.setMinValue(1);
                endDayPick.setMaxValue(getDayTS(getEndYear(),getEndMonth()));
                endMon = picker.getValue();
            }
        });
        endDayPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("TAG", "onValueEndChange: " + picker.getValue());
                endDay = picker.getValue();
            }
        });
        endHourPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("TAG", "onValueEndChange: " + picker.getValue());
                endHOUR = picker.getValue();
            }
        });
        endMinPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("TAG", "onValueEndChange: " + picker.getValue());
                endMin = picker.getValue();
            }
        });
        endSecPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Log.i("TAG", "onValueEndChange: " + picker.getValue());
                endSec = picker.getValue();
            }
        });


        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    private int getDayTS(int year, int month){
        int day = 30;
        boolean flag = false;
        switch (year % 4){
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag? 28:29;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    public int getYear(){
        return startYearPick.getValue();
    }

    public int getMonth(){
        return startMonPick.getValue();
    }

    public int getEndYear(){
        return endYearPick.getValue();
    }

    public int getEndMonth(){
        return endMonPick.getValue();
    }
    public boolean saveConfig(){
//        SPHelper.putPreference(getActivity(), KEY_SCREENBRIGHT_START_HOUR, startHour);
//        SPHelper.putPreference(getActivity(), KEY_SCREENBRIGHT_START_MIN, startMin);
//        SPHelper.putPreference(getActivity(), KEY_SCREENBRIGHT_END_HOUR, endHOUR);
//        SPHelper.putPreference(getActivity(), KEY_SCREENBRIGHT_END_MIN, endMin);

        return true;
    }
    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }




    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };
    @Override
    public void onClick(View v) {
        dismissAllowingStateLoss();
        if (mConfigCallback != null){
            mConfigCallback.modeClick(v.getId());
            Log.i("TAG", "onClick: ============" + v.getId());
        }
    }


    public interface ConfigCallBack {

        void modeClick(int id);

        void onConfigOk();

        void onConfigCancel();
    }



}
